import axios from "axios";
import router from '../router'

export default function(options){
        // var defaultHeaders={Authorization:sessionStorage.getItem('token')};
        // options.headers = Object.assign({},defaultHeaders,options.headers || {});
        options.headers = Object.assign({}, { Authorization: sessionStorage.getItem("token") }, options.headers || "");

        return axios(options)
                .then(({status,statusText,data:{code,data,msg}}) => {
                        if(status === 200){
                                switch(code){
                                        case 200:
                                               return data;
                                        case 401:
                                                sessionStorage.clear();
                                                router.push('/login?target='+router.currentRoute.fullPath);
                                                throw new Error(msg);
                                        case 199:
                                        case 404:
                                        case 500:
                                                throw new Error(msg);

                                }
                        }else throw  new Error(statusText);
                })
                .catch(function(error){
                        alert(error.message);
                        return Promise.reject(error)
                });
}